#define _WIN32_WINNT	0x0501
#include <windows.h>
#include "CScene.h"
//#include "PopFile.h"
#include "globals.h"
//#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void SetupPixelFormat (HDC);

int WINAPI WinMain (HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	static TCHAR	szAppName[] = TEXT ("Base");
	WNDCLASS		wndclass;
	HWND			hwnd;
	MSG				msg;

	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInstance;
	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground = NULL;
	wndclass.lpszMenuName  = NULL;//MAKEINTRESOURCE (IDR_MENU);
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass (&wndclass))
		return 0;

	hwnd = CreateWindow (szAppName,
						 szAppName,
						 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 CW_USEDEFAULT,
						 NULL,
						 NULL,
						 hInstance,
						 NULL);

	if (!hwnd)
		return 0;

	ShowWindow (hwnd, SW_SHOW);
	UpdateWindow (hwnd);

	g_scene.Init ();

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}

	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
	static HDC		hdc;
	static HGLRC	hrc;
	static WORD		width, height;
	static tagPOINT	pt;

	static TCHAR szFileName[100], szTitleName[100];

	switch (msg)
	{
	case WM_CREATE:
		hdc = GetDC (hwnd);
		SetupPixelFormat (hdc);
		hrc = wglCreateContext (hdc);
		wglMakeCurrent (hdc, hrc);

		//g_fontID = g_msg.addNewFont(hdc);
		//g_msg.setFontColor(1.0f, 0.0f, 0.0f, g_fontID);

		SetTimer (hwnd, 0, 10, NULL);
		return 0;
	case WM_COMMAND:
		switch (LOWORD (wp))
		{
		//case ID_OPEN:
		//	if (PopFileOpenDlg (hwnd, szFileName, szTitleName))
		//	{

		//		g_scene.LoadModel (wstring(szFileName));
		//	}
		//	break;
		}
		return 0;
	case WM_CHAR:
		if(wp==VK_ESCAPE)
			DestroyWindow(hwnd);
		else if(wp=='f'||wp=='F')
		{
			//if (PopFileOpenDlg (hwnd, szFileName, szTitleName))
			//	g_scene.LoadModel (wstring(szFileName));
		}

		g_scene.m_controller.pressKey(wp);
		InvalidateRect (hwnd, NULL, FALSE);
		
		break;
	case WM_KEYDOWN:
		switch (wp)
		{
		case VK_SPACE:
			InvalidateRect (hwnd, NULL, FALSE);
			break;
		case VK_RIGHT:
			InvalidateRect (hwnd, NULL, FALSE);
			break;
		case VK_LEFT:
			InvalidateRect (hwnd, NULL, FALSE);
			break;
		default:
			break;
		}
		return 0;
	case WM_LBUTTONDOWN:
		pt.x = LOWORD (lp);
		pt.y = HIWORD (lp);
		//g_scene.m_controller.LeftPress (pt.x, height - pt.y);
		return 0;
	case WM_LBUTTONUP:
		pt.x = LOWORD (lp);
		pt.y = HIWORD (lp);
		//g_scene.m_controller.LeftRelease (pt.x, height - pt.y);
		return 0;
	case WM_RBUTTONDOWN:
		pt.x = LOWORD (lp);
		pt.y = HIWORD (lp);
		//g_scene.m_controller.RightPress (pt.x, height - pt.y);
		return 0;
	case WM_RBUTTONUP:
		pt.x = LOWORD (lp);
		pt.y = HIWORD (lp);
		//g_scene.m_controller.RightRelease (pt.x, height - pt.y);
		return 0;
	case WM_MOUSEWHEEL:
		//g_scene.m_controller.Wheel (HIWORD (wp));
		return 0;
	case WM_TIMER:
		/*if (g_scene.m_controller.beLDrag ())
		{
			GetCursorPos (&pt);
			ScreenToClient (hwnd, &pt);
			//g_scene.m_controller.LeftDrag (pt.x, height - pt.y);
		}
		
		if (g_scene.m_controller.beRDrag ())
		{
			GetCursorPos (&pt);
			ScreenToClient (hwnd, &pt);
			//g_scene.m_controller.RightDrag (pt.x, height - pt.y);
		}
		
		if (g_scene.m_controller.beWheel ())
			g_scene.m_controller.Zoom ();
		*/
		InvalidateRect (hwnd, NULL, FALSE);
		return 0;
	case WM_SIZE:
		width  = LOWORD (lp);
		height = HIWORD (lp);
		g_scene.Resize (width, height);
		return 0;
	case WM_PAINT:
		g_scene.Render ();
		SwapBuffers (hdc);
		ValidateRect (hwnd, NULL);
		return 0;
	case WM_DESTROY:
		wglMakeCurrent (hdc, NULL);
		wglDeleteContext (hrc);
		KillTimer (hwnd, 0);
		PostQuitMessage (0);
		return 0;
	}

	return DefWindowProc (hwnd, msg, wp, lp);
}

void SetupPixelFormat (HDC hdc)
{
	int iPixelFormat;

	static PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof (PIXELFORMATDESCRIPTOR),  // size
		1,  // version
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0,
		16,  // depth buffer
		0,
		0,
		0,
		0,
		0, 0, 0
	};

	iPixelFormat = ChoosePixelFormat (hdc, &pfd);

	SetPixelFormat (hdc, iPixelFormat, &pfd);
}
